﻿/*!
 * Copyright (c), 上海沃恩信息科技有限公司.
 * All rights reserved.
 * Licensed under BSD (https://www.pittypat.work/bsd.html).
 */

using System.IO;
using System.Net;

namespace Pittypat.Web
{
    /// <summary>
    /// 向客户端相应一个 HTTP 状态代码。
    /// </summary>
    sealed class StatusMessage : Message
    {
        private HttpStatusCode status;
        private string description;

        /// <summary>
        /// 使用状态代码和描述初始化 <see cref="StatusMessage"/> 类的新实例。
        /// </summary>
        /// <param name="status">状态代码。</param>
        /// <param name="description">描述信息。</param>
        internal StatusMessage (HttpStatusCode status, string description = null)
        {
            this.status = status;
            this.description = string.IsNullOrEmpty(description) ? GetDefaultDescription(status) : description;
        }

        /// <summary>
        /// 根据状态代码获取默认的状态描述信息。
        /// </summary>
        /// <param name="status">状态代码。</param>
        /// <returns>对应状态代码的状态消息。</returns>
        static string GetDefaultDescription(HttpStatusCode status)
        {
            switch (status)
            {
                case HttpStatusCode.Continue:
                    return "Continue";
                case HttpStatusCode.SwitchingProtocols:
                    return "Switching Protocols";
                case HttpStatusCode.OK:
                    return "OK";
                case HttpStatusCode.Created:
                    return "Created";
                case HttpStatusCode.Accepted:
                    return "Accepted";
                case HttpStatusCode.NonAuthoritativeInformation:
                    return "Non-Authoritative Information";
                case HttpStatusCode.NoContent:
                    return "No Content";
                case HttpStatusCode.ResetContent:
                    return "Reset Content";
                case HttpStatusCode.PartialContent:
                    return "Partial Content";
                case HttpStatusCode.MultipleChoices:
                    return "Multiple Choices";
                case HttpStatusCode.MovedPermanently:
                    return "Moved Permanently";
                case HttpStatusCode.Found:
                    return "Found";
                case HttpStatusCode.SeeOther:
                    return "See Other";
                case HttpStatusCode.NotModified:
                    return "Not Modified";
                case HttpStatusCode.UseProxy:
                    return "Use Proxy";
                case HttpStatusCode.Unused:
                    return "Unused";
                case HttpStatusCode.TemporaryRedirect:
                    return "Temporary Redirect";
                case HttpStatusCode.BadRequest:
                    return "Bad Request";
                case HttpStatusCode.Unauthorized:
                    return "Unauthorized";
                case HttpStatusCode.PaymentRequired:
                    return "Payment Required";
                case HttpStatusCode.Forbidden:
                    return "Forbidden";
                case HttpStatusCode.NotFound:
                    return "Not Found";
                case HttpStatusCode.MethodNotAllowed:
                    return "Method Not Allowed";
                case HttpStatusCode.NotAcceptable:
                    return "Not Acceptable";
                case HttpStatusCode.ProxyAuthenticationRequired:
                    return "Proxy Authentication Required";
                case HttpStatusCode.RequestTimeout:
                    return "Request Timeout";
                case HttpStatusCode.Conflict:
                    return "Conflict";
                case HttpStatusCode.Gone:
                    return "Gone";
                case HttpStatusCode.LengthRequired:
                    return "Length Required";
                case HttpStatusCode.PreconditionFailed:
                    return "Precondition Failed";
                case HttpStatusCode.RequestEntityTooLarge:
                    return "Request Entity Too Large";
                case HttpStatusCode.RequestUriTooLong:
                    return "Request Uri Too Long";
                case HttpStatusCode.UnsupportedMediaType:
                    return "Unsupported Media Type";
                case HttpStatusCode.RequestedRangeNotSatisfiable:
                    return "Requested Range Not Satisfiable";
                case HttpStatusCode.ExpectationFailed:
                    return "Expectation Failed";
                case HttpStatusCode.UpgradeRequired:
                    return "Upgrade Required";
                case HttpStatusCode.InternalServerError:
                    return "Internal Server Error";
                case HttpStatusCode.NotImplemented:
                    return "Not Implemented";
                case HttpStatusCode.BadGateway:
                    return "Bad Gateway";
                case HttpStatusCode.ServiceUnavailable:
                    return "Service Unavailable";
                case HttpStatusCode.GatewayTimeout:
                    return "Gateway Timeout";
                case HttpStatusCode.HttpVersionNotSupported:
                    return "HttpVersion Not Supported";
                default:
                    return "";
            }
        }

        /// <summary>
        /// 获取状态代码。
        /// </summary>
        public HttpStatusCode Status
        {
            get
            {
                return this.status;
            }
        }

        /// <summary>
        /// 获取状态描述。
        /// </summary>
        public string Description
        {
            get
            {
                return this.description;
            }
        }

        /// <summary>
        /// 什么都不做。
        /// </summary>
        /// <param name="writer">目标文本流。</param>
        public override void Write(TextWriter writer)
        {
            if (writer != null)
            {
                writer.Write("这是一个 HTTP 状态消息，不应该解析为 JSON。");
            }
        }
    }
}
